#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
@Author: 李吉凯
@Email: lijk34925@hundsun.com
@Time: 2022/8/8 9:21
@File: check.py
"""
from qturboframe.lib.webdriver import SeleniumError
from qturboframe.public_ui.base_element import BaseElement
from BladeFerm.Lib.Common import logger


class SwitchType(BaseElement):
    switch_class_flag = 'h-switch'
    switch_class_status = 'h-switch-checked'

    def __init__(self, element_para, father=None, timeout=5):
        super().__init__(element_para, father, timeout)
        class_list = self._element.get_attribute('class').split(' ')
        logger.debug(f'switch class_list: {class_list}')
        self.switchStatus = False
        if self.switch_class_flag not in class_list:
            self._element = self._element.find_element_by_attribute('xpath', './span[contains(@class,"h-switch")]')
        if self.switch_class_status in class_list:
            self.switchStatus = True

    def get_cur_status(self):
        '''
        判断check控件是否是选中状态
        :return:
        '''
        return self.switchStatus

    def set_status(self, flag=True):
        '''
        设置状态
        :param flag:
        :return:
        '''
        cur_flag = self.get_cur_status()
        if cur_flag != flag:
            self._element.click()
        logger.info(f'设置状态为：{flag}')

    def is_default(self, flag=True):
        '''
        检查是否是默认的状态
        :param flag:
        :return:
        '''
        cur_flag = self.get_cur_status()
        if flag == cur_flag:
            return True
        else:
            raise SeleniumError(f'Switch控件的默认状态与期望{flag}不符！')